package com.shitou.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.shitou.pojo.Message;

public abstract class BaseAction extends ActionSupport implements SessionAware,ServletRequestAware, ServletResponseAware{

	private static final long serialVersionUID = 1L;
	public static final String RELOAD = "reload";
	public static final String INDEX = "index";
	public static final String ISSUES = "issues";
	public static final String ASSIGNMENT = "assignment";
	public static final String COMMENT = "comment";
	public static final String MILESTONE = "milestone";
	public static final String DOCUMENT = "document";
	public static final String PROJECT = "project";
	public static final String JSON_TYPE = "application/json;charset=UTF-8";
	protected Map<String, Object> session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	public String execute() throws Exception {
		return list();
	}
	
	public abstract String list() throws Exception;
	public abstract String save() throws Exception;
	public abstract String input() throws Exception;
	public abstract String del() throws Exception;

	public void putSuccessMessage(String message) {
		Message msg = new Message();
		msg.setType(Message.SUCCESS);
		msg.setMessage(message);
		setSessionAttribute("msg", msg);
	}
	
	public void putErrorMessage(String message) {
		Message msg = new Message();
		msg.setType(Message.ERROR);
		msg.setMessage(message);
		setSessionAttribute("msg", msg);
	}
	
	public void putAttentionMessage(String message) {
		Message msg = new Message();
		msg.setType(Message.ATTENTION);
		msg.setMessage(message);
		setSessionAttribute("msg", msg);
	}
	
	public void putInfomationMessage(String message) {
		Message msg = new Message();
		msg.setType(Message.INFOMATION);
		msg.setMessage(message);
		setSessionAttribute("msg", msg);
	}
	
	public String getRealPath() {
		return getRequest().getSession().getServletContext().getRealPath("/");
	}
	
	public Object getSessionAttribute(String name) {
		return session.get(name);
	}
	
	public void setSessionAttribute(String name, Object obj) {
		session.put(name, obj);
	}
	
	public void removeSessionAttribute(String name) {
		session.remove(name);
	}
	
	public void renderJson(String jsonString) throws IOException {
		response.setContentType(JSON_TYPE);
		PrintWriter out = response.getWriter();
		out.print(jsonString);
		out.flush();
		out.close();
	}
	
	//getter and setter
	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setServletRequest(HttpServletRequest request){
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response){
		this.response = response;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public HttpServletResponse getResponse() {
		return response;
	}
}
